﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using SQLExecute;


public partial class list_majors_subject : System.Web.UI.Page
{
    MajorSubject m = new MajorSubject();
    static int nMajorSubjectID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["username"] != null)
                BindGrid();
            else Response.Redirect("~/login/login.aspx");

        }
        
    }
    protected void BindGrid()
    {
        m.Get_List_Major_Subject2(gdMajorSubject);
    }
    protected void gdDisciplines_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdMajorSubject.PageIndex = e.NewPageIndex;
        BindGrid();
     
    }

    private void Del_Major_Subject(int id)
    {
        //string sql = "update tbl_majorsubject2 set active=0 where id='" + id + "'";
        string sql = "delete tbl_majorsubject2 where id='" + id + "'";

        SqlHelper.ExecuteNonQuery(Common.strConnect, CommandType.Text, sql);
        BindGrid();
    }
    protected void gdDisciplines_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("lnkButtonDelete");
            l.Attributes.Add("onclick", "javascript:return "
                            + "confirm('Are you sure you want to delete this record " + DataBinder.Eval(e.Row.DataItem, "majorsubject_name") + "')");
        }
    }
    
    protected void lnkLogout_Click(object sender, EventArgs e)
    {
      
        Session.Clear();
        Response.Redirect("~/login/login.aspx");
    }
    protected bool ExistOfMajor(string majorname)
    {
        string sql = "select majorsubject_name from tbl_majorsubject2 where majorsubject_name='" + majorname + "'";
        SqlDataReader r = SqlHelper.ExecuteReader(Common.strConnect, CommandType.Text, sql);
        if (r.HasRows)
            return true;
        else return false;

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (txtMajorSubjectNew.Text != "")
        {
          
            if (!ExistOfMajor(txtMajorSubjectNew.Text))
            {
                m = new MajorSubject(txtMajorSubjectNew.Text);
                m.Insert_Major_Subject();
                BindGrid();
                txtMajorSubjectNew.Text="";
               // EmptyTextbox();
            }
            else
            {
                omb.ShowMessage("Major subject name is exist!.", "Info");
            }
        }
        else omb.ShowMessage("Please enter major subject name!.", "Info");

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {

        if (txtMajorSubjectEdit.Text != "")
        {
            m.MajorID = nMajorSubjectID;
            m.MajorSubjectName = txtMajorSubjectEdit.Text;
            m.Update_Major_Subject();
        }
        
        BindGrid();
    }

    protected void MajorSubject_Click(object sender, EventArgs e)
    {
       
        LinkButton lnkButton = sender as LinkButton;
        GridViewRow row = (GridViewRow)lnkButton.NamingContainer;
        nMajorSubjectID = Convert.ToInt16(gdMajorSubject.DataKeys[row.RowIndex].Value.ToString());
        string sql = "select * from tbl_majorsubject2 where id='" + nMajorSubjectID + "'";
        SqlDataReader r = SqlHelper.ExecuteReader(Common.strConnect, CommandType.Text, sql);
        if (r.HasRows)
        {

            while (r.Read())
            {
                txtMajorSubjectEdit.Text = r["majorsubject_name"].ToString();
            }
        }
        popupEdit.Show();
       

    }
    protected void gdMajorSubject_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    
    protected void gdMajorSubject_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Del_Major_Subject(nMajorSubjectID);
    }
    protected void gdMajorSubject_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {

            nMajorSubjectID = Convert.ToInt16(e.CommandArgument);
            Del_Major_Subject(nMajorSubjectID);
            BindGrid();
        }
    }
}
